<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *客户列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'客户列表'),
);
$this->model('model')->checkjuese(5);
$juese_id=$_SESSION['juese_id']==5?$_SESSION['juese_id']:5;
$workerids=$this->model('model')->getworkerids($juese_id);
$return['stores']=$this->getall('store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
$return['workers']=$this->getall('worker',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
//客户列表
if($_RQ['op']=='list'){
	$this->model('member')->up_haidata();
	$return['params']=array();
	$params=array('worker'.$juese_id.'id'=>$workerids,'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['jibie_type'] && $_RQ['jibie_type']!='all'){
		$params['jibie_type']=intval($_RQ['jibie_type']);
	}
	$return['params']['jibie_type']=$_RQ['jibie_type']?:'all';
	if($_RQ['status'] && $_RQ['status']!='all'){
		if($_RQ['status']==-1){
			$params['status']=array(-1);
		}else{
			$params['status']=intval($_RQ['status']);
		}
		$return['params']['status']=$_RQ['status'];
	}
	if($params['status']==110){
		$params['status']=array(100,110);
		$pcd=pdo_getcolumn('jy_yxw_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['storeid']),'pcd');
		$pcd=json_decode($pcd,true);
		$city=$pcd['city'];
		$storeids=pdo_getall('jy_yxw_store',array('uniacid'=>$_SESSION['uniacid'],'pcd like'=>'%"'.$city.'%'),array('id'),'id');
		if($storeids){
			$storeids=array_keys($storeids);
		}else{
			$storeids=$_SESSION['storeid'];
		}
		$params['storeid']=$storeids;
	}
	if($_RQ['storeid']){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if($_RQ['xueli']){
		$params['xueli']=intval($_RQ['xueli']);
		$return['params']['xueli']=$_RQ['xueli'];
	}
	if($_RQ['from1_type']){
		$params['from1_type']=intval($_RQ['from1_type']);
		$return['params']['from1_type']=$_RQ['from1_type'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if($_RQ['up_time']){
		$time_range=cfc('tools')->base64_decode($_RQ['up_time'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['up_time between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['up_time']=$time_range;
	}
	if($_RQ['status']==110){
		unset($params['worker5id']);
		$batch=array(
			//array('type'=>'post','url'=>'jy_yxw/juese5.list/get/','title'=>'一键领取客户'),
		);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:30;
	if($_RQ['psize']){
		$return['params']['psize']=$_RQ['psize'];
	}
	$list=pdo_getslice('jy_yxw_member',$params,array($pindex,$psize),$total,$fields,'','up_time asc, huifang_time asc, id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=$this->model('member')->member_show($item['id']);
			if($item['from_type']==1){
				$item['from_typetitle']=$this->member_from1_type[$item['from1_type']];
			}elseif($item['from_type']==2){
				$item['from_typetitle']=$this->member_from2_type[$item['from2_type']];
			}else{
				$item['from_typetitle']=$this->member_from_type[$item['from_type']];
			}
			$item['up_time']=$item['up_time']?date('Y/m/d',$item['up_time']):'无';
			$item['createtime']=date('Y/m/d',$item['createtime']);
			if(!$item['oldworker5id']){
				pdo_update('jy_yxw_member',array('oldworker5id'=>$item['worker5id']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['id']));
			}
			if($item['status']>0){
				$this->model('uplog')->upworkerlog($item,1,true);
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'export'=>'export',
		'list'=>array('data'=>'list','del_message'=>'确认删除该客户吗？','total'=>$total,'psize'=>$psize,'del_op'=>'del','del_one'=>false,'delall'=>false,'edit'=>false,'list'=>array(
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'storeid','title'=>'所属门店','type'=>'select2','options'=>'stores'),
			array('name'=>'jibie_type','title'=>'客户级别','type'=>'radio2','options'=>'jibie_type'),
			array('name'=>'from_typetitle','title'=>'渠道','type'=>'string'),
			array('title'=>'网络员','name'=>'worker3id','type'=>'select2','options'=>'workers'),
			array('title'=>'前台专员','name'=>'worker4id','type'=>'select2','options'=>'workers'),
			array('title'=>'电客员','name'=>'worker5id','type'=>'select2','options'=>'workers'),
			array('title'=>'销售员','name'=>'worker6id','type'=>'select2','options'=>'workers'),
			array('title'=>'体验员','name'=>'worker7id','type'=>'select2','options'=>'workers'),
			array('title'=>'试用员','name'=>'worker8id','type'=>'select2','options'=>'workers'),
			array('name'=>'status','title'=>'状态','type'=>'radio2','options'=>'status'),
			array('name'=>'up_time','title'=>'最近跟进时间','type'=>'string'),
			array('name'=>'createtime','title'=>'添加时间','type'=>'string'),
			array('type'=>'href','action'=>'/member.genjin_log/onelist/','title'=>'查看跟进记录'),
		),'action'=>array(
			array('type'=>'href','action'=>'jy_yxw/xiaoshou.setgenjin_log/set5/','title'=>'处理电客情况','show'=>array('status'=>array(-1,1,2,3,30,31))),
			array('type'=>'post','action'=>'jy_yxw/juese5.list/get/','title'=>'领取客户','show'=>array('status'=>array(100,110))),
			
		),'batch'=>$batch),
		'search'=>array(
			array('type'=>'string','width'=>'10','title'=>'手机号','name'=>'mobile','help'=>''),
			array('type'=>'string','width'=>'10','title'=>'客户名称','name'=>'title','help'=>''),
			array('name'=>'xueli','width'=>'10','title'=>'学历','type'=>'radio','options'=>'xueli_type'),
			array('name'=>'from1_type','width'=>'10','title'=>'渠道','type'=>'radio','options'=>'from1_type'),
			array('name'=>'status','width'=>10,'title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'up_time','width'=>15,'title'=>'时间段','type'=>'time_range')
		),
	);
	if($_RQ['status']==30){
		$htmls['list']['list'][]=array('name'=>'huifang_time','title'=>'回访时间','type'=>'time');
	}
	$return['status']=$this->member_status1;
	unset($return['status'][0]);
	$return['status']['all']='全部';
	$return['jibie_type']=$this->member_jibie_type;
	$return['from_type']=$this->member_from_type;
	$return['from1_type']=$this->member_from1_type;
	$return['from2_type']=$this->member_from2_type;
	$return['xueli_type']=$this->xueli_type;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$this->model('member')->add($postdata);
		exi('操作成功','','/juese5.list//status=0');
	}
	if($_RQ['id']){
		$item=pdo_get('jy_yxw_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
		if($item['birth']){
			$item['age']=date('Y')-$item['birth'];
		}
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'edit'=>$this->model('member')->edit_item('',5),
	);
	$return['sex']=$this->sex;
	$return['xueli_type']=$this->xueli_type;
	$return['from_type']=$this->member_from_type;
	$return['member_from1_type']=$this->member_from1_type;
	$return['member_from2_type']=$this->member_from2_type;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	//客户列表
	$return['params']=array();
	$params=array('worker'.$juese_id.'id'=>$workerids,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['jibie_type']!='all'){
		$params['jibie_type']=intval($_RQ['jibie_type']);
	}
	$return['params']['jibie_type']=$_RQ['jibie_type'];
	if($_RQ['status']){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['storeid']){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if($_RQ['xueli']){
		$params['xueli']=intval($_RQ['xueli']);
		$return['params']['xueli']=$_RQ['xueli'];
	}
	if($_RQ['from1_type']){
		$params['from1_type']=intval($_RQ['from1_type']);
		$return['params']['from1_type']=$_RQ['from1_type'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$list=pdo_getall('jy_yxw_member',$params,$fields);
	if($list){
		foreach($list as &$item){
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			$item['jibie_type']=$this->member_jibie_type[$item['jibie_type']];
			$item['from_type']=$this->member_from_type[$item['from_type']];
			$item['status']=$this->member_status[$item['status']];
			$item['xueli']=$this->xueli_type[$item['xueli']];
		}
	}
	$header=array(
		'id'=>'客户ID',
		'title'=>'客户名称',
		'mobile'=>'手机号',
		'jibie_type'=>'级别',
		'from_type'=>'渠道',
		'xueli'=>'学历',
		'status'=>'状态',
		'createtime'=>'注册时间',
	);
	model('core/tools')->export2excel($header,$list,'客户数据');
}
if($_RQ['op']=='setstatus4'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=$post['memberid'];
		$update=array('up_time'=>time());
		if(!$post['setstatus']){
			$update['status']=4;
			$post['type']=4;
			$post['status']=2;
		}
		if($post['setstatus']==1){
			$update['status']=2;
			$post['type']=3;
			$post['status']=2;
		}
		$post['remark']='客户已意向';
		$this->model('uplog')->addgenjin_log($post);
		pdo_update('jy_yxw_member',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('操作成功！','','//list/status=4');
	}
	if($_RQ['id']){
		$item=pdo_get('jy_yxw_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
		$item['setstatus']=0;
		$item['memberid']=$item['id'];
		unset($item['id']);
	}else{
		exi('请先选择客户','error');
	}
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'处理意向',
	);
	$htmls['edit']=array(
		array('type'=>'radio','title'=>'意向状态','name'=>'setstatus','options'=>'status'),
		array('type'=>'text','title'=>'未按时意向原因','name'=>'remark','show'=>array('status'=>array(1))),
	);
	$return['status']=array('已意向','未按时意向');	
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			$this->model('member')->check_del($id,'xiaoshou');
			pdo_delete('jy_yxw_member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='get'){
	$id=$_RQ['id'];
	$update=array('worker5id'=>$_SESSION['workerid'],'worker3id'=>0,'worker4id'=>0,'worker6id'=>0,'storeid'=>$_SESSION['storeid'],'status'=>1,'up_time'=>time());
	$uplog=array(
					'memberid'=>$id,
					'remark'=>'公海领取成功',
					'status'=>1,
					'type'=>2,
	);
	$this->model('uplog')->addgenjin_log($uplog);
	pdo_update('jy_yxw_member',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	exi('领取成功！','','/juese5.list//status=110');
}